package com.gw.booking.common;

import net.sf.json.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

public class SmsSender {
	static Logger logger = LoggerFactory.getLogger(SmsSender.class);
	public static String restUrl = "https://open.ucpaas.com/ol/sms/sendsms";
	public static String appId = "bdeae3089b724b37ba405b3ac90d8d18";
	public static String accountSid = "010132bef137b62bb5dc7e6c2bcb3e80";
	public static String authToken = "ef368609a7f81841796b2a128608a650";
	private static Properties config = null;
	
	public static enum SmsKey {
		/**找回密码*/forget_password,
		/**修改密码*/change_password,
		/**修改邮箱*/change_email,
		/**修改手机号*/change_mobile,
		/**注册会员*/register_mobile,
		/**商城订单支付*/pay_store_order,
		/**通用积分余额不足*/fuyou_balance_warning,
		/**企业初始密码*/add_enterprise,
		/**绑定成员*/binding_member
	}
	
	/**
	 * 根据消息id取得短信内容
	 * @param messageKey
	 * @param argv
	 * @return
	 */
	public static String getSmsContent(SmsKey messageKey, String... argv) {
		try {
			if (config == null) {
				config = new Properties();
				config.load(new InputStreamReader(SmsSender.class.getResourceAsStream("/sms.properties"), "UTF-8"));
				logger.info("加载短信模板成功。");
			}
		} catch (IOException e) {
			logger.error("加载短信模板成功。");
		}
		
		return config.getProperty(messageKey.name());
	}
	
    /**
     * 发送短信
     */
    public static String sendSmsByKey(String mobile, SmsKey messageKey, String...argv) {
    	try {
    		String templateId = getSmsContent(messageKey);

			sendSms(mobile, templateId, StringUtils.join(argv));
		} catch (Throwable e) {
			logger.error("发送短信失败。" + mobile + getSmsContent(messageKey, argv));
		}
    	
    	return getSmsContent(messageKey, argv);
    }
	
    /**
     * 发送短信
     */
    private static void sendSms(String mobile, String templateId, String param) throws Throwable{

		JSONObject requestDataJson = new JSONObject();
		requestDataJson.put("sid", accountSid);
		requestDataJson.put("token", authToken);
		requestDataJson.put("appid", appId);
		requestDataJson.put("templateid", templateId);
		requestDataJson.put("mobile", mobile);
		if (StringUtils.isNotEmpty(param)) {
			requestDataJson.put("param", param);
		}

        PostMethod postMethod = new PostMethod(restUrl);
        StringRequestEntity se = new StringRequestEntity(requestDataJson.toString(), "application/json; charset=UTF-8", "UTF-8");
        postMethod.setRequestEntity(se);

        HttpClient httpClient = new HttpClient();
        int statusCode = httpClient.executeMethod(postMethod);
        System.out.println("发送" + postMethod.getResponseBodyAsString());
        if(statusCode != 200){
            throw new ApiException(MessageCode.CODE_API_EXCEPTION);
        }
    }
    
    public static void main(String[] argv) {
    	try {
    		System.out.println(sendSmsByKey("15900636884", SmsKey.register_mobile, "test"));
		} catch (Throwable e) {
			e.printStackTrace();
		}
    }
}
